package nl.changer.audiowifedemo_ohos;

import nl.changer.audiowifedemo_ohos.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;

import java.io.*;

public class MainAbility extends Ability {
    public final static String TEST_AUDIO_FILE_PATH = "/data/data/nl.changer.audiowifedemo_ohos/test_audio.mp3";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        try {
            //文件初始化，由于OHOS暂不支持跳转到系统文件管理器，先内置一份音频文件
            initFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void initFile() throws IOException {
        ResourceManager resourceManager = getResourceManager();
        RawFileEntry entry = resourceManager.getRawFileEntry("resources/rawfile/test_audio.mp3");
        try {
            Resource resource = entry.openRawFile();
            BufferedInputStream in = null;
            BufferedOutputStream out = null;
            in = new BufferedInputStream(resource);
            out = new BufferedOutputStream(new FileOutputStream(TEST_AUDIO_FILE_PATH));
            int len = -1;
            byte[] b = new byte[1024];
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
